package com.ruoyi.system.controller.front;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.GaAlarm;
import com.ruoyi.system.domain.GaUser;
import com.ruoyi.system.dto.input.AlarmDetailGetInput;
import com.ruoyi.system.dto.input.AlarmInsertPostInput;
import com.ruoyi.system.dto.input.AlarmUpdatePostInput;
import com.ruoyi.system.dto.output.AlarmHistoryListGetOutput;
import com.ruoyi.system.enservice.IAlarmEnService;
import com.ruoyi.system.service.IGaAlarmService;
import com.ruoyi.system.service.IGaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static java.util.stream.Collectors.toList;

@RestController
@RequestMapping("/front/alarm")
public class AlarmController extends BaseController {
    @Autowired
    private IGaAlarmService gaAlarmService;

    @Autowired
    private IGaUserService gaUserService;

    @Autowired
    private IAlarmEnService alarmEnService;

    /**
     * 学生更新报警信息
     * @author xuankai
     * @param input 报警对象
     * @return 是否更新成功
     **/
    @PostMapping("/update")
    public AjaxResult updateAlarm(AlarmUpdatePostInput input) {
        GaAlarm alarm = gaAlarmService.selectGaAlarmById(input.getId());
        alarm.setContent(input.getContent());
        alarm.setTitle(input.getTitle());
        alarm.setType(input.getType());
        alarm.setState(input.getState());

        if(gaAlarmService.updateGaAlarm(alarm) == 0) {
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    /**
     * 学生添加报警信息
     * @author xuankai
     * @param input 报警对象
     * @return 是否添加成功
     **/
    @PostMapping("/insert")
    public AjaxResult insertAlarm(AlarmInsertPostInput input) {
        GaAlarm alarm = new GaAlarm();
        alarm.setCreateUser(input.getCreateUser());
        alarm.setContent(input.getContent());
        alarm.setTitle(input.getTitle());
        alarm.setType(input.getType());
        alarm.setCreatePosition(input.getCreatePosition());

        alarm.setState("0");
        if(gaAlarmService.insertGaAlarm(alarm) == 0) {
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    /**
     * 查询报警历史接口
     * @author xuankai
     * @param studentId 学生ID
     * @return 报警记录列表
     **/
    @GetMapping("/historyList/{studentId}")
    public AjaxResult getHistoryList(@PathVariable("studentId") Long studentId) {
        GaUser user = gaUserService.selectGaUserById(studentId);

        GaAlarm alarm = new GaAlarm();
        alarm.setCreateUser(studentId);
        startPage();
        List<GaAlarm> list = gaAlarmService.selectGaAlarmList(alarm);
        List<AlarmHistoryListGetOutput> result = list.stream().map(al -> {
            AlarmHistoryListGetOutput tmp = new AlarmHistoryListGetOutput();
            tmp.setAlarmId(al.getId());
            tmp.setAlarmType(al.getType());
            tmp.setAlarmName(al.getTitle());
            tmp.setAlarmStatus(al.getState());
            return tmp;
        }).collect(toList());
        return AjaxResult.success(result);
    }

    /**
     * 通过学生ID和案件ID查询该案件的详细信息
     * @author xuankai
     * @param dto 包含学生ID与案件ID的请求体
     * @return 案件的详细信息
     **/
    @GetMapping("/detail")
    public AjaxResult getDetail(AlarmDetailGetInput dto) {
        return AjaxResult.success(alarmEnService.getDetail(dto));
    }
}
